package com.edu.service.impl;

import com.edu.bean.Book;
import com.edu.mapper.BookMapper;
import com.edu.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Override
    public List<Book> getAllBook() {
        return bookMapper.selectAllBook();
    }

    @Override
    public List<Book> getBookByPage(int pageNum, int pageSize) {
        return bookMapper.selectAllBook(pageNum,pageSize);
    }

    @Override
    public int updateBookById(Book book) {
        System.out.println(book);
        return bookMapper.updateBook(book);
    }

    @Override
    public int getBookCountByBook(Book book) {
        if (book.getPutDate()!=null){
            // 创建一个 SimpleDateFormat 对象，指定目标格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用 SimpleDateFormat 的 format 方法来格式化 Date 对象
            book.setStrDate(formatter.format(book.getPutDate()));
            System.out.println(formatter.format(book.getPutDate()));
        }
        return bookMapper.selectBookCountByBook(book);
    }

    @Override
    public List<Book> getBookByPageAndBook(int page, int pageSize,Book book) {
        if (book.getPutDate()!=null){
            // 创建一个 SimpleDateFormat 对象，指定目标格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用 SimpleDateFormat 的 format 方法来格式化 Date 对象
            book.setStrDate(formatter.format(book.getPutDate()));
        }
        return bookMapper.selectAllBookByBook(page,pageSize,book);
    }

    @Override
    public int deleteBookById(int bookId) {
        return bookMapper.deleteBookById(bookId);
    }

    @Override
    public int getBookCount() {
        return bookMapper.selectBookCount();
    }
}
